package com.yu.admin.modules.system.controller;

import com.yu.admin.common.annotation.NeedPermission;
import com.yu.admin.common.util.session.AdminSessionUtil;
import com.yu.admin.modules.system.mapper.SysUserMapper;
import com.yu.admin.modules.system.mapper.UserRoleMapper;
import com.yu.admin.modules.system.pojo.SysUser;
import com.yu.admin.modules.system.pojo.vo.SysUserVo;
import com.yu.admin.modules.system.service.impl.SysUserServiceImpl;
import com.yu.common.exception.ServiceException;
import com.yu.common.util.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @date 2020-11-29 18:33:56
 */
@Api(tags = "系统：系统管理员")
@RestController
@RequestMapping("/api-system/sysUser")
public class SysUserController {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysUserServiceImpl sysUserService;

    @Resource
    private UserRoleMapper userRoleMapper;

    @ApiOperation("查询全部")
    @NeedPermission("system:sysUser:list")
    @GetMapping("/list")
    public R<List<SysUserVo>> getAllSysUsers() throws ServiceException {
        List<SysUserVo> users = sysUserMapper.getAllUserVOs();
        for (SysUserVo user : users)
            user.setRoles(userRoleMapper.selectRolesOfUser(user.getId()));
        return R.ok(users);
    }

    @ApiOperation("根据id查询")
    @NeedPermission("system:sysUser:info")
    @GetMapping("/{sysUserId}")
    public R detail(@PathVariable("sysUserId") Integer sysUserId) {
        return R.ok(sysUserMapper.selectById(sysUserId));
    }

    @ApiOperation("增")
    @NeedPermission("system:sysUser:add")
    @PostMapping("")
    public R add(@RequestBody SysUser sysUser) throws ServiceException {
        sysUser.setPassword("123456");
        return R.ok(sysUserMapper.insert(sysUser));
    }

    @ApiOperation("改")
    @NeedPermission("system:sysUser:update")
    @PutMapping("")
    public R update(@RequestBody SysUser sysUser) throws ServiceException {
        return R.ok(sysUserMapper.updateById(sysUser));
    }

    @ApiOperation("批量删")
    @NeedPermission("system:sysUser:delete")
    @DeleteMapping("/batch")
    public R deleteBatch(@RequestBody List<Integer> sysUserIdList) throws ServiceException {
        return R.ok(sysUserMapper.deleteBatchIds(sysUserIdList));
    }

    @ApiOperation("更新账号激活状态")
    @NeedPermission("system:sysUser:update")
    @PutMapping("/status/{sysUserId}/{status}")
    public R updateStatus(@ApiParam("sysUserId") @PathVariable Integer sysUserId,
                          @ApiParam("status") @PathVariable Boolean status) throws ServiceException {
        SysUser sysUser = new SysUser();
        sysUser.setId(sysUserId);
        sysUser.setStatus(status);
        return R.ok(sysUserMapper.updateById(sysUser));
    }

    @ApiOperation("重置密码为123456")
    @NeedPermission("system:sysUser:update")
    @PutMapping("/resetPassword/{sysUserId}")
    public R resetPassword(@ApiParam("sysUserId") @PathVariable Integer sysUserId) throws ServiceException {
        SysUser sysUser = new SysUser();
        sysUser.setId(sysUserId);
        sysUser.setPassword("123456");
        return R.ok(sysUserMapper.updateById(sysUser));
    }

    @ApiOperation("角色授权")
    @NeedPermission("system:sysUser:update")
    @PutMapping("/role/{userId}")
    public R setRoleOfSysUser(@PathVariable Integer userId, @RequestBody List<Integer> roleIds) {
        sysUserService.setRoleOfSysUser(userId, roleIds);
        return R.ok();
    }
}
